<template>
  <div style="margin: 20px">
    <div style="display: flex; justify-content: space-between; font-size: 16px; border-bottom: 1px solid #ccc; padding-bottom: 5px">
      <div>
        <span class="el-icon-tickets" style="margin-right: 15px"></span>
        <span>动作配置</span>
      </div>
      <div class="top-button">
        <el-button size="mini" type="primary" @click="handleAdd">新增</el-button>
      </div>
    </div>

    <div style="margin-top: 5px">
      <el-form ref="gridHeadLayout" :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item>
          <el-input
            v-model="formInline.actionName"
            size="mini"
            placeholder="请输入动作名称">
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-select v-model="formInline.actionType" size="mini" placeholder="请选择动作类型">
            <el-option
                v-for="item in actionDict"
                :key="item.value"
                :label="item.label"
                :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item class="top-button">
          <el-button size="mini" @click="handleSearch" icon="el-icon-search"></el-button>
          <el-button size="mini" @click="handleEmpty" icon="el-icon-refresh-right"></el-button>
        </el-form-item>
      </el-form>
    </div>

    <div style="max-height: calc(100vh - 285px);height: calc(100vh - 285px); overflow: auto;">
      <div style="max-height: calc(100vh - 285px);height: calc(100vh - 285px); overflow: auto;">
        <el-row v-if="data.length" style="margin-top: 20px">
          <div class="warning-card">
            <el-col :span="6" v-for="(item, index) in data" :key="index">
              <el-card shadow="hover" style="font-size: 14px; padding-bottom: 10px; margin-right: 10px;margin-bottom: 10px">
                <div style="margin-bottom: 10px">
                  <div style="padding: 20px 20px 5px 20px">
                    <div class="point-head" style="position: relative;font-size: 18px; font-weight: bold; margin-bottom: 10px;">
                      {{ item.actionName }}
                      <el-tooltip class="item" effect="dark" content="编辑" placement="top">
                        <a style="font-size: 14px; position: absolute; right: 0" @click="rowEdit(item)">
                          <span class="el-icon-edit" style="font-size: 18px;color: #1677FF"></span>
                        </a>
                      </el-tooltip>
                    </div>
                    <div class="point-content" style="margin-bottom: 10px">
                      <div style="margin: 16px 6px 0 0px">
                        <span style="color: #646a73">动作类型：</span>
                        <span style="color: #1677ff;">
                        <dict-tag :type="DICT_TYPE.ACTION_TYPE" :value="item.actionType" />
                      </span>
                      </div>
                      <div style="margin: 16px 6px 0 0px">
                        <span style="color: #646a73">创建时间：</span>
                        <span style="color: #1677ff;">{{ parseTime(item.createTime) }}</span>
                      </div>
                    </div>
                    <el-divider></el-divider>
                  </div>

                  <div style="padding: 0 20px 0 20px;">
                    <div style="float: left">
                      <el-button size="mini" style="margin: 5px"
                                 @click="rowRemove(item)">删除
                      </el-button>
                    </div>
                    <div style="float: right">
                      <el-button type="text" @click="handleView(item)" size="small">更多信息</el-button>
                    </div>
                  </div>
                </div>

              </el-card>
            </el-col>
          </div>
        </el-row>
      </div>

      <el-pagination
          background
          :current-page="page.currentPage"
          :page-sizes="[8, 16, 32, 64, 128]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="page.total"
          :page-size="page.pageSize"
          :pager-count="5"
          @size-change="changeSize"
          @current-change="changeCurrent"
          style="float: right; margin: 20px 0;">
      </el-pagination>
    </div>

    <ActionAddDialog
      ref="actionAddDialog"
      @restOnLoad="restOnLoad">
    </ActionAddDialog>
  </div>

</template>

<script>
import ActionAddDialog from "@/views/warning/action/components/actionAddDialog.vue";
import {page as getPage, remove} from "@/api/warning/warning-action-api";

export default {
  name: "action",
  computed: {
    ids() {
      let ids = [];
      this.selectionList.forEach(ele => {
        ids.push(ele.id);
      });
      return ids.join(",");
    },
  },
  components: {
    ActionAddDialog
  },
  data() {
    return {
      tableLoading: false,
      data: [],
      selectionList: [], // 表格多选框
      formInline: {},
      actionDict: [], // 动作类型字典
      page: {
        pageSize: 8,
        currentPage: 1,
        total: 0
      },
    }
  },
  mounted() {
    this.onLoad(this.page, {});
    this.actionDict = this.$store.state.dict.dictDatas.action_type
  },
  methods: {
    handleSearch () {
      this.page.currentPage = 1
      this.onLoad(this.page, this.formInline)
    },
    handleEmpty () {
      this.formInline = {}
      this.onLoad(this.page)
    },
    // 批量删除
    handleRomove() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择数据");
        return;
      }
      this.$confirm("确定将选择数据删除", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        remove(this.ids).then(res => {
          if (res.code === 0) {
            // 刷新表格数据并重载
            this.data = [];
            // 表格数据重载
            this.onLoad(this.page);
            this.$message.success("操作成功");
          }
        })
      })
    },
    // 行删除
    rowRemove(row) {
      this.$confirm("确定将选择数据删除", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        remove(row.id).then(res => {
          if (res.code === 0) {
            // 刷新表格数据并重载
            this.data = [];
            // 表格数据重载
            this.onLoad(this.page);
            this.$message.success("操作成功");
          }
        })
      })
    },
    // 行编辑
    rowEdit(row) {
      this.$refs.actionAddDialog.editPoint(row.id)
    },
    handleView(row) {
      this.$refs.actionAddDialog.viewPoint(row.id)
    },
    handleAdd() {
      this.$refs.actionAddDialog.addPoint()
    },
    // 表单返回
    restOnLoad() {
      // 重新刷新页面
      this.onLoad(this.page, {})
    },
    handleSelectionChange (selection) {
      this.selectionList = selection
    },
    changeCurrent (val) {
      this.page.currentPage = val
      this.onLoad(this.page, {})
    },
    changeSize (val) {
      this.page.pageSize = val
      this.onLoad(this.page, {})
    },
    indexMethod (index) {
      return index + 1
    },
    // 清空选项框
    // toggleSelection() {
    //   this.$refs.productTable.clearSelection();
    // },
    onLoad(page, params = {}) {
      this.page = page;
      this.tableLoading = true;
      getPage(
        page.currentPage,
        page.pageSize,
        Object.assign(params, this.formInline)
      ).then(res => {
        const data = res.data;
        this.page.total = data.total;
        this.data = data.records;
        this.tableLoading = false;
        // this.toggleSelection();
      });
    },
  }
};
</script>
<style scoped>
.warning-card ::v-deep .el-card__body {
  padding: 0 !important;
}

.warning-card ::v-deep .el-divider--horizontal {
  margin: 0;
}

.top-button ::v-deep .el-button + .el-button {
  margin-left: 0;
}
</style>
